
#include <gtest/gtest.h>

#include "nane/lib/nane.h"
#include "nane/core/crt/filesystem.h"
#include "nane/core/function/Functor.h"
#include "nane/ScriptSystem/ScriptSystem.h"
#include "nane/core/IO.h"

static int s_testExportFunction( int _arg )
{
    return _arg;
}

TEST( NaneTest, ScriptSystemCheck )
{
    ASSERT_TRUE( nane::Initialize() );
    nane::ReadFilePtr file = nane::FileSystem::OpenReadFile("test.lua");
    ASSERT_TRUE( file != nane::NullPtr );
    nane::ScriptSystem scriptSystem;
    EXPECT_TRUE( scriptSystem.Initialize() );
    nane::ScriptModule scriptModule = scriptSystem.LoadModule("test", file);
    file = nane::NullPtr;    
    ASSERT_TRUE( scriptModule != nane::NullPtr );
    //nane::ScriptParamsBuffer<int, int> args(1, 2);
    //nane::ScriptParamsBuffer<int> results;
    //int resNum = 0;
    //int testValue = 0;
    nane::ScriptObject scriptObject = scriptModule.ImportObject("TestFunction");
    ASSERT_TRUE( scriptObject != nane::NullPtr );
    //EXPECT_TRUE( scriptObject->invoke(nane::InputPtr(&args), nane::OutputPtr(&results)) );
    int result1 = scriptObject.Invoke<int, int, int>(1, 2);
    EXPECT_EQ( 3, result1 );
    //results.seek(0);
    //nane::IO<nane::IOSafe>::read(&results, &resNum);
    //EXPECT_EQ( 1, resNum );
    //nane::EScriptValueType scriptValueType = nane::EScriptValueType::Unknown;
    //nane::TScriptValue scriptValue;
    //nane::ScriptSerializer::deserialize(&results, &scriptValueType, &scriptValue);
    //EXPECT_TRUE( scriptValueType == nane::EScriptValueType::Integer || scriptValueType == nane::EScriptValueType::Float );
    //EXPECT_TRUE( nane::ScriptWrapT<int>::getValue(scriptValue, scriptValueType, &testValue) );
    //EXPECT_EQ( 3, testValue );
    scriptObject = nane::NullPtr;
    EXPECT_TRUE( scriptModule.ExportFunction(NULL, "CTestFunction", &s_testExportFunction) );
    //resNum = 0;
    //results.seek(0);
    int result2 = scriptModule.ImportObject("TestImportFunction").Invoke<int>();
    EXPECT_EQ( 5, result2 );
    scriptModule = nane::NullPtr;
    scriptSystem.Shutdown();
    //nane::FileSystem::closeFile(file);
    nane::Shutdown();
}
